<?php
/**
 * Created by PhpStorm.
 * User: lihaitao
 * Date: 2019/9/9
 * Time: 下午8:01
 */

namespace app\index\controller;


use think\Db;

class My extends Base
{
    //我的账户
    public function myCoin()
    {
        $uid = input("uid");
        $account = Db::name("user")->where("id",$uid)->find();
        $p = substr($account['phone'],0,3)."****".substr($account['phone'],7,4);
        $account['phone'] = $p;
        $account['msg'] = "获取成功";
        return $this->_toSuccess($account);
    }


    //我的评论
    public function myComment()
    {
        $uid = request()->post("uid");

        $page = \request()->post("page") ?? 1;
        $pageSize = ($page - 1) * 15;

        $lists = Db::name("comment")
            ->alias("c")
            ->join("user u","c.uid = u.id")
            ->join("digital d","c.digital_id = d.id")
            ->where("c.uid",$uid)
            ->field("u.nickname,u.avatar,c.content,c.created_at,d.first_image,d.id,d.type")
            ->limit($pageSize,15)
            ->select();
        $data['msg']  = "获取成功";
        $data['list'] = $lists;

        return $this->_toSuccess($data);
    }

    //我的点赞
    public function myLike()
    {
        $uid = request()->post("uid");

        $page = \request()->post("page") ?? 1;
        $pageSize = ($page - 1) * 15;

        $lists = Db::name("like")
            ->alias("c")
            ->join("user u","c.uid = u.id")
            ->join("digital d","c.digital_id = d.id")
            ->where("c.uid",$uid)
            ->field("u.nickname,u.avatar,c.created_at,d.first_image,d.id,d.type")
            ->limit($pageSize,15)
            ->select();
        $data['msg']  = "获取成功";
        $data['list'] = $lists;

        return $this->_toSuccess($data);
    }
    
    //我的积分明细
    public function myPoint()
    {
        $uid = request()->post("uid");

        $page = \request()->post("page") ?? 1;
        $pageSize = ($page - 1) * 15;

        $point = Db::name("point")
            ->where("uid",$uid)
            ->limit($pageSize,15)
            ->select();

        $data['msg']  = "获取成功";
        $data['list'] = $point;

        return $this->_toSuccess($data);

    }
    
    //我的-规则明细
    public function rule()
    {
        $msg = "活动规则:评论得1金币,最高可得3金币,每两个点赞可得1金币,每天最多3金币。10金币=1元人民币  每天最高可得10金币";
        $msgT = "活动规则:姚头叹琪,琪虎楠下";

        return $this->_toSuccess(['ruleMsg' => $msgT , 'msg' => '获取成功~']);
    }
    
    //手机号授权
    public function mobileAuth()
    {
        require '../extend/wxBizDataCrypt.php';
        //Loader::import('wxBizDataCrypt');
        $code = input("code");
        $uid  = input("uid");

        if (!$code || !$uid) {
            return $this->_toError(['msg' => '数据异常']);
        }
        $arr = $this->getOpenMsg($code);


        $session_key = $arr['session_key'];

        $appId = config()["wx"]["appid"];
        $pc = new \WXBizDataCrypt($appId,$session_key);
        $errCode = $pc->decryptData(input('encryptedData'),input('iv'),$data);

        if ($errCode == 0) {

            $phoneNumArr = json_decode($data,true);

            if (!$phoneNumArr['phoneNumber']) {
                return $this->_toError(['msg' => '手机号获取失败']);
            }

            $user = Db::name("user")->where("id",$uid)->find();
            if (!$user['phone']) {
                Db::name("user")->where("id",$uid)->setInc('point',5);
                Db::name("point")
                    ->insert([
                        'uid'    => $uid,
                        'type'   => 1,
                        'number' => 5,
                        'digital_id' => "",
                        'remark' => '绑定手机送金币',
                        'created_at' => date("Y-m-d H:i:s",time())
                    ]);
            }

            Db::name("user")
                ->where('id',$uid)
                ->update([
                    'phone' => $phoneNumArr['phoneNumber']
                ]);

            return $this->_toSuccess(['msg' => '手机号授权成功']);

        } else {
            return $this->_toError(['msg' => '手机号授权失败']);
        }
    }

    //获取用户详情
    public function getOpenMsg($code)
    {
        $appId  = config()["wx"]["appid"];
        $secret = config()["wx"]["secret"];

        $url   = "https://api.weixin.qq.com/sns/jscode2session?appid={$appId}&secret={$secret}&js_code={$code}&grant_type=authorization_code";
        $grant = file_get_contents($url);
        $arr   = json_decode($grant,true);

        return $arr;
    }
}